<!--  -->
<template>
  <div class="refund">
    <Nav title="退换货">
      <template v-slot:left>
        <span class="back" @click=" $router.back()"></span>
      </template>
    </Nav>
    <!-- <template v-for="(item,index) in 1">
      <div :key="index">
        <div class="mt-12 flex border-bottom">
          <img class="product-img ml-14" :src="refundInfo.image" alt="">
          <div class="product-info ml-22">
            <div class="f-14 flex justify-between">
              <span class="title ellipsis-line2">{{refundInfo.goods_name}}</span>
              <span class="mr-16">￥{{refundInfo.price}}</span>
            </div>
            <div class="color-gray f-14 mt-10 flex justify-between">
              <span>{{refundInfo.sku_name}}</span>
              <span class="mr-16">x{{refundInfo.num}}</span>
            </div>
            <div class="mt-24">七天退换</div>
          </div>
        </div>
      </div>
    </template>-->
    <!-- <div class="flex justify-between aligin-center product-price f-12">
      <span class="color-gray">
        共{{1}}件商品
      </span>
      <span>
        <span class="f-14 ml-10 color-red">换货</span>
      </span>
    </div>-->

    <div class="divide-line"></div>
    <div class="refund-reason f-14">
      <div>售后原因（必填）</div>
      <van-field v-model="refundReason" type="textarea" placeholder="请输入售后原因" rows="5"/>
      <cube-validator
        ref="refundReason"
        v-model="valid[0]"
        :model="refundReason"
        :rules="commonRules"
      ></cube-validator>
    </div>
    <div class="divide-line"></div>
    <div class="refund-img f-14">
      <div>请上传商品照片（1-3张）</div>
      <van-uploader
        class="mt-18 ml-14"
        v-model="fileList"
        multiple
        :max-count="3"
        :after-read="afterRead"
      />
      <cube-validator ref="fileList" v-model="valid[1]" :model="fileList" :rules="commonRules"></cube-validator>
    </div>
    <div class="divide-line"></div>
    <div class="refund-item">
      <div class="title">请选择售后内容（单选）</div>
      <div class="input-wrap color-gray">
        <input class="input" id="radio" type="radio" value="0" v-model="refundOption">
        <span class="icon"></span>
        <label for="radio">部分换货</label>
      </div>
      <div class="input-wrap color-gray">
        <input class="input" id="radio1" type="radio" value="1" v-model="refundOption">
         <span class="icon"></span>
        <label for="radio1">全部换货</label>
      </div>
      <div class="input-wrap color-gray">
        <input class="input" id="radio2" type="radio" value="2" v-model="refundOption">
         <span class="icon"></span>
        <label for="radio2">部分退款</label>
      </div>
      <div class="input-wrap color-gray">
        <input class="input" id="radio3" type="radio" value="3" v-model="refundOption">
        <span class="icon"></span>
        <label for="radio3">全部退款</label>
      </div>
    </div>
    <div class="btn-wrapper flex justify-center">
      <div
        class="btn flex justify-center aligin-center color-white"
        :class="flag?'bg-gray':''"
        @click="confirm"
      >确定</div>
    </div>
  </div>
</template>

<script>
import Nav from "../../components/Nav";
import { refundOrder, orderRefund } from "../../api/api";
import axios from "axios";
import config from "../../utils/config";
import compress from "../../utils/picCompress";
export default {
  components: { Nav },
  data() {
    return {
      refundOption: "2",
      refundReason: "", //退款原因
      fileList: [],
      refundInfo: {},
      valid: [undefined, undefined],
      commonRules: {
        required: true
      },
      flag: false
    };
  },
  computed: {},
  watch: {},
  methods: {
    // 上传图片
    afterRead(file) {
      compress(file)
        .then(_file => {
          let formData = new FormData();
          formData.append("img", _file);
          return axios.post(config.baseUrl + "/api/image/upload", formData, {
            headers: {
              authorization: "bearer " + this.$store.state.token
            }
          });
        })
        .then(({ data: { result } }) => {
          this.fileList.splice(this.fileList.length - 1, 1, {
            url: result.url
          });
        })
        .catch(err => {});
    },
    confirm() {
      if (this.flag) {
        return;
      }
      const refundReason = this.$refs.refundReason.validate();
      const fileList = this.$refs.fileList.validate();

      Promise.all([refundReason, fileList]).then(() => {
        if (this.valid.every(item => item)) {
          orderRefund({
            order_goods_id: this.refundInfo.order_goods_id,
            image: JSON.stringify(this.fileList),
            refund_reason: this.refundReason
          }).then(res => {
            this.$notify("申请成功！");
            this.flag = true;
            setTimeout(() => {
              this.$router.back();
            }, 1000);
          });
        }
      });
    }
  },
  created() {},
  mounted() {
    refundOrder({ order_no: this.$route.params.id }).then(
      ({ data: { result } }) => {
        this.refundInfo = result;
      }
    );
  },
  updated() {}, //生命周期 - 更新之后
  destroyed() {} //生命周期 - 销毁完成
};
</script>
<style lang='less' scoped>
@import url("../../assets/style/mixin"); //引入公共css类
.refund {
  padding-bottom: 64px;
  height: 100%;
  box-sizing: border-box;
  .border-bottom {
    padding-bottom: 20px;
    border-bottom: 1px solid rgba(234, 234, 234, 1);
  }
  .product-img {
    height: 93px;
    width: 93px;
    border-radius: 5px;
  }
  .product-info {
    flex: 1;
    .title {
      height: 36px;
      line-height: 18px;
    }
  }
  .product-price {
    padding: 0 16px;
    height: 43px;
  }
  .refund-reason {
    height: 173px;
    box-sizing: border-box;
    padding: 15px;
  }
  .refund-img {
    padding: 15px;
  }
  .btn-wrapper {
    margin-top: 60px;
  }
  .btn {
    width: 150px;
    height: 44px;
    border-radius: 5px;
    font-size: 16px;
    .bg-1();
  }
  .bg-gray {
    background: gray;
  }
  .refund-item {
    padding: 20px 0 0 19px;
    .title{
      margin-bottom: 22px;
      font-size: 14px;
    }
    .input-wrap{
      margin-bottom: 9px;
      position: relative;
      display: flex;
    }
    .icon{
      position: relative;
      margin-right: 13px;
      width: 10px;
      height:10px;
      border:2px solid rgba(225,225,225,1);
      border-radius:50%;
    }
    .input:checked+.icon{
      &::after{
        content: "";
        display: block;
        height: 100%;
        width: 100%;
        border-radius:50%;
        background: #EA373F;
      }
    }
    .input {
      position: absolute;
      top: 0;
      left: 0;
      width: 12px;
      height: 12px;
      opacity: 0;
      z-index: 1;
    }
  }
}
</style>